import Vue from 'vue'
import Router from 'vue-router'
import {mymessage} from '../tools/mymessage'

Vue.use(Router)

const routes = [
  {
    path: '/',
    name: 'Login',
    component: () => import('../view/login/Login'), // 路由懒加载
    meta: {
      label: '登录',
      index: 0,
      requireAuth: false
    }
  },
  {
    path: '/Register',
    name: 'Register',
    component: () => import('../view/register/Register'), // 路由懒加载
    meta: {
      label: '注册',
      index: 0,
      requireAuth: false
    }
  },
  {
    path: '/MainPage',
    name: 'MainPage',
    component: () => import('../view/MainPage'), // 路由懒加载
    meta: {
      label: '首页',
      index: 0,
      requireAuth: true
    },
    children: [
      {
        path: '/BookShelf',
        name: 'BookShelf',
        menu: true,
        component: () => import('../view/mainPages/Bookshelf'), // 路由懒加载
        meta: {
          label: '书架',
          index: 0,
          requireAuth: true
        }
      },
      {
        path: '/BookCity',
        name: 'BookCity',
        component: () => import('../view/mainPages/BookCity'), // 路由懒加载
        meta: {
          label: '书城',
          index: 0,
          requireAuth: true
        }
      },
      {
        path: '/Forum',
        name: 'Forum',
        component: () => import('../view/mainPages/Forum'), // 路由懒加载
        meta: {
          label: '主题讨论区',
          index: 0,
          requireAuth: true
        }
      },
      {
        path: '/Learned',
        name: 'Learned',
        // component: () => import('../view/mainPages/Learned'), // 路由懒加载
        component: () => import('../view/mainPages/Forum'),
        meta: {
          label: '心得分享',
          index: 0,
          requireAuth: true
        }
      },
      {
        path: '/ReportCenter',
        name: 'ReportCenter',
        component: () => import('../view/report/ReportCenter'),
        meta: {
          label: '报表中心',
          index: 0,
          requireAuth: true
        }
      }
    ],
    redirect: '/BookShelf'
  },
  {
    path: '/userSetting',
    name: 'userSetting',
    component: () => import('../view/userSetting/UserSetting'),
    meta: {
      label: '个人设置',
      index: 0,
      requireAuth: true
    }
  },
  {
    path: '/EBook',
    name: 'EBook',
    component: () => import('../view/ebook/Index'), // 路由懒加载
    meta: {
      label: '阅读',
      index: 0,
      requireAuth: true
    }
  },
  {
    path: '/ThemeForum',
    name: 'ThemeForum',
    component: () => import('../view/forum/ThemeForum'),
    meta: {
      label: '主题讨论',
      index: 0,
      requireAuth: true
    }
  },
  {
    path: '/LearnedForum',
    name: 'LearnedForum',
    component: () => import('../view/forum/LearnedForum'),
    meta: {
      label: '心得体会',
      index: 0,
      requireAuth: true
    }
  },
  {
    path: '/DesignReport',
    name: 'DesignReport',
    component: () => import('../view/report/DesignReport'),
    meta: {
      label: '设计报表',
      index: 0,
      requireAuth: true
    }
  }
]

const router = new Router({
  routes
})

router.beforeEach(async (to, from, next) => {
  /** 判断用户是否已经登录 */
  // if (store.getters.isLogin) {
  //   document.title = to.matched[0].meta.label;
  //   // /!** 已经登录情况下访问 /login, 则直接进入 /admin *!/
  //   if (to.path === '/') {
  //     next()
  //     mymessage.done()
  //   } else {
  //     next()
  //   }
  // } else {
  //   if (noPermission(to.path) || (to.meta.requireAuth === false)) {
  //     next()
  //   } else {
  //     next('/')
  //     mymessage.warn('请先登录')
  //     // NProgress.done()
  //   }
  // }
  document.title = to.matched[0].meta.label
  if (sessionStorage.getItem('isLogin') === 'true') {
    if (haveThisPage(to.path)) {
      next()
    } else {
      next('/')
      mymessage.error('路径出错')
    }
  } else {
    if (noPermission(to.path) || (to.meta.requireAuth === false)) {
      next()
    } else {
      next('/')
      mymessage.warning('请先登录')
    }
  }
})

let haveThisPage = function (path) {
  let haveThisPage = true
  for (let i = 0; i < routes.length; i++) {
    if (routes[i].children !== undefined) {
      for (let j = 0; j < routes[i].children.length; j++) {
        if (routes[i].children[j].path === path) {
          haveThisPage = true
          return haveThisPage
        }
      }
    }
    if (routes[i].path === path) {
      haveThisPage = true
      return haveThisPage
    } else {
      haveThisPage = false
    }
  }
  return haveThisPage
}

function noPermission (path) {
  return ['/404'].indexOf(path) > -1
}

export default router
